package org.lc.stk.web.controller.question;

import jakarta.validation.Valid;

import org.lc.stk.service.question.ChoiceQuestionService;
import org.lc.stk.web.dto.question.ChoiceQuestionDTO;
import org.lc.stk.web.response.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/v1/questions/choice")
@Tag(name = "选择题管理", description = "单选题和多选题的增删改查接口")
public class ChoiceQuestionController {

    private final ChoiceQuestionService choiceQuestionService;

    public ChoiceQuestionController(ChoiceQuestionService choiceQuestionService) {
        this.choiceQuestionService = choiceQuestionService;
    }

    @PostMapping
    @Operation(summary = "创建选择题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<ChoiceQuestionDTO>> createQuestion(
            @Parameter(description = "选择题信息") @Valid @RequestBody ChoiceQuestionDTO questionDTO) {
        ChoiceQuestionDTO created = choiceQuestionService.createQuestion(questionDTO);
        return ResponseEntity.ok(ApiResponse.success(created));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新选择题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<ChoiceQuestionDTO>> updateQuestion(
            @Parameter(description = "选择题ID") @PathVariable Long id,
            @Parameter(description = "选择题信息") @Valid @RequestBody ChoiceQuestionDTO questionDTO) {
        ChoiceQuestionDTO updated = choiceQuestionService.updateQuestion(id, questionDTO);
        return ResponseEntity.ok(ApiResponse.success(updated));
    }
}